// 权限控制
import router from './router'
import { getCookie } from './utils/auth'
import store from './store'

const WHITE_LIST = ['/login', '/404']

// 处理一级路由权限数据
function getFirstRoutePerms(permsArr) {
  const newArr = permsArr.map(item => {
    return item.split(':')[0]
  })
  return [...new Set(newArr)]
}

// 处理二级路由权限数据
function getSecondRoutePerms(permsArr) {
  const newArr = permsArr.map(item => {
    const arr = item.split(':')
    return `${arr[0]}:${arr[1]}`
  })
  return [...new Set(newArr)]
}

router.beforeEach(async(to, from, next) => {
  const token = getCookie('park_token')
  // 有token
  if (token) {
    next()
    // 获取用户信息
    if (!store.state.user.profile.id) {
      // 1. 调用action函数获取用户权限数据
      const perms = await store.dispatch('user/getUserProfile')
      // 2. 把后端的权限数组格式化成我们自己的俩个权限数据
      console.log('当前的权限数据为:', perms)
      const firstRoutePerms = getFirstRoutePerms(perms)
      console.log(firstRoutePerms)
      const secondRoutePerms = getSecondRoutePerms(perms)
      console.log(secondRoutePerms)
    }
  } else {
    // 没有token
    if (WHITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
